<meta args="config,catName"/>
<form s="nda,datac,valid" s-data="config" id="form">
    <table class="table table-bordered table-striped">
        <tr>
            <td style="width: 10%;">关键字</td>
            <td style="width: 40%">
                <input autocomplete="off" type="text" style="width: 100%" name="q" class="form-control"/>
            </td>
            <td style="width: 10%;">店铺Dsr评分</td>
            <td style="width: 40%">
                <input autocomplete="off" type="text" name="startDsr" s-valid-error="number()" class="form-control"/>
            </td>
        </tr>
        <tr>
            <td>链接形式</td>
            <td>
                <select name="platform" class="form-control">
                    <option value="PC">PC</option>
                    <option value="WL" selected>无线</option>
                </select>
            </td>
            <td>消费者保障</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" type="checkbox" name="needPrepay" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
        </tr>
        <tr>
            <td>佣金比率下限</td>
            <td>
                <input autocomplete="off" type="number" value="1000" name="startTkRate" placeholder="2000表示20.00%"
                       s-valid-error="number()" class="form-control"/>
            </td>
            <td>佣金比率上限</td>
            <td>
                <input autocomplete="off" type="number" name="endTkRate" placeholder="3000表示30.00%"
                       s-valid-error="number()" class="form-control"/>
            </td>
        </tr>
        <tr>
            <td>起始价</td>
            <td>
                <input autocomplete="off" type="number" name="startPrice" s-valid-error="number()"
                       class="form-control"/>
            </td>
            <td>结束价</td>
            <td>
                <input autocomplete="off" type="number" s-valid-error="require()" name="endPrice"
                       s-valid-error="number()" class="form-control"/>
            </td>
        </tr>
        <tr>
            <td>天猫</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" type="checkbox" name="tmall" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
            <td>海外</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" type="checkbox" name="overseas" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
        </tr>
        <tr>
            <td>所在地</td>
            <td>
                <input autocomplete="off" type="text" name="itemloc" class="form-control"/>
            </td>
            <td>排序</td>
            <td>
                <select class="form-control" s="select"
                        name="sort"
                        s-build="S.get('/api/enum/valuesMap/core.enums.TbkDgMaterialOptionalSort')">
                    <option value="">默认排序</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>后台类目ID</td>
            <td>
                <div class="input-group" style="width: 100%">
                    <input autocomplete="off" type="text" name="cat" id="cat" class="form-control"/>
                    <div class="input-group-btn">
                        <input type="button" class="btn btn-default" value="选择" s-click="selectLocalItemCat()"/>
                    </div>

                </div>
                <div class="help-block">用,分割，最大10个，该ID可以通过taobao.itemcats.get接口获取到</div>
            </td>
            <td>是否有优惠券</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" checked type="checkbox" name="hasCoupon" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
        </tr>
        <tr>
            <td>物料ID</td>
            <td>
                <select class="form-control" s="select"
                        id="taobaoMaterialCat"
                        s-build="S.get('/api/taobao-material-cat/list')">
                    <option value="">物料类目</option>
                </select>
                <select class="form-control" s="select"
                        name="materialId"
                        s-data="config"
                        s-datac-ng="true"
                        s-build-switch="'off-on'"
                        id="taobaoMaterial"
                        s-select="{value: 'materialId'}"
                        s-build="S.get('/api/taobao-material-cat/material/list/' + ($taobaoMaterialCat.val() || 0))">
                    <option value="">淘宝物料</option>
                </select>
            </td>
            <td>包邮</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" type="checkbox" name="needFreeShipment" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
        </tr>
        <tr>
            <td>牛皮癣</td>
            <td>
                <select name="npxLevel" class="form-control">
                    <option value="1">不限</option>
                    <option value="2">无</option>
                    <option value="3">轻微</option>
                </select>
            </td>
            <td>成交转化高于行业均值</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" type="checkbox" name="includePayRate30" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
        </tr>
        <tr>
            <td>好评率高于行业均值</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" type="checkbox" name="includeGoodRate" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
            <td>退款率低于行业均值</td>
            <td>
                <label class="s-checkbox-switch">
                    <input autocomplete="off" type="checkbox" name="includeRfdRate" value="true"/>
                    <span class="s-checkbox-indicator"></span>
                </label>
            </td>
        </tr>
    </table>
</form>

<script>
    config = $.extend({
        startTkRate: 2000,
        sort: 'TOTAL_SALES_DES',
        endPrice: 200
    }, config || {})
    S.getConfig = function () {
        var deferred = $.Deferred();
        $form.validate().done(function () {
            deferred.resolve(Smart.serializeToObject($form.node))
        })
        return deferred;
    }

    S.meta.btns = [
        {
            name: "选择",
            style: "btn btn-primary",
            click: function() {
                S.getConfig().done(function(config) {
                    S.close(config)
                })
            }
        },
        {
            name: "关闭",
            style: "btn btn-default",
            click: function() {
                S.close();
            }
        }
    ]

    function selectTopItemCat() {
        S.popupOpen("/top/itemCatSelector.html", {multi: true}).done(function (itemCats) {
            if (itemCats && itemCats.length) {
                var val = $cat.node.val()
                var ids = itemCats.map(function (ic) {
                    return ic.cid
                }).join(",")
                if (val != '') {
                    val += ',' + ids
                } else {
                    val = ids
                }
                $cat.node.val(val)
            }
        })
    }

    function selectLocalItemCat(catName) {
        S.popupOpen("/top/localItemCatSelector.html", {multi: true, catName: catName}).done(function (itemCats) {
            if (itemCats && itemCats.length) {
                var val = $cat.node.val()
                var ids = itemCats.map(function (ic) {
                    return ic.cid
                }).join(",")
                if (val != '') {
                    val += ',' + ids
                } else {
                    val = ids
                }
                $cat.node.val(val)
            }
        })
    }

    S.on('s-ready', function(){
      S.cascade($taobaoMaterialCat, $taobaoMaterial);
    })

    if (catName) {
        selectLocalItemCat(catName);
    }
</script>